﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace ScannerApp
{
    public class AppConfig
    {
        public static string WebServiceUrlKey = "WebServiceUrl";
        public static string UserNameKey = "UserName";
        public static string PasswordKey = "Password";
        public static string RememberMeKey = "RememberMe";

        static AppConfig _instance;
        public static AppConfig Instance
        {
            get {

                if (_instance == null)
                {
                    _instance = new AppConfig();
                    _instance.Init();
                }
                return AppConfig._instance;
            }
        }

        private XmlDocument config;
        private void Init()
        {
            config = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            config.Load(path + "/App.config");       
        }

        
        public String GetSettingValue(string key)
        {
            XmlNode node = config.SelectSingleNode("//appSettings/add[@key='" + key + "']");
            return (node.Attributes["value"].Value);
        }

        public void SetSettingValue(string key,string value)
        {
            XmlNode node = config.SelectSingleNode("//appSettings/add[@key='" + key + "']");
            node.Attributes["value"].Value = value;
        }

        public void Save()
        {
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            config.Save(path + "/App.config");       
        }
    }
}
